package com.geega.geetest.geetest;

import com.geega.geetest.aopLog.PrintLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

/**
 * 滑块验证
 * @author anhong
 * @date 2021-12-06
 */
@RestController
@RequestMapping("/geetest")
@Api(tags = "滑块验证码")
public class CaptchaResource {

    private final Logger log = LoggerFactory.getLogger(CaptchaResource.class);

    private final CaptchaService captchaService;

    public CaptchaResource(CaptchaService captchaService) {
        this.captchaService = captchaService;
    }


    /**
     * 初始化滑块验证码
     * @param:
     * @date 2021/12/6 14:18
    */
    @ApiOperation("初始化滑块验证码")
    @PostMapping(value = "/captcha")
    @PrintLog("初始化滑块验证码")
    public ResponseEntity<Object> captchaInit() {
        log.info("REST request to captchaInit : {}", "初始化滑块");
        return ResponseEntity.ok(captchaService.initCaptcha());
    }

    /**
     * 滑块验证码验证
     * @param: challenge
     * @author anhong
     * @date 2021/12/6 14:55
    */
    @ApiOperation("滑块验证码验证")
    @PostMapping(value = "/verify")
    public ResponseEntity<Boolean> captchaVerify(@RequestParam("challenge") String challenge) {
        log.info("REST request to captchaVerify : {}", "滑块验证码验证");
        return Optional.ofNullable(challenge)
           .map(obj-> ResponseEntity.ok(captchaService.verify(challenge)))
           .orElseGet(()-> ResponseEntity.ok(true));
    }

}
